package world.geometry;
import javax.media.j3d.QuadArray;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Point3f;

class FloorCeiling extends QuadArray {

	private boolean DEBUG = false;
	
    // names for vertices, eg. rtf_idx is the index of the
    // start of the right top front vertex
    private final int rtf_idx = 0;
    private final int ltf_idx = 1;
    private final int lbf_idx = 2;
    private final int rbf_idx = 3;
    private final int rtb_idx = 4;
    private final int ltb_idx = 5;
    private final int lbb_idx = 6;
    private final int rbb_idx = 7;

    // names for faces
    private final int top_idx = 0;
    private final int bottom_idx = 1;

    // names for coordinate axes
    private final int x_idx = 0;
    private final int y_idx = 1;
    private final int z_idx = 2;

    private float[] verts = {

        // right top front
	 1.0f,  1.0f, 1.0f,
	// left top front
	-1.0f,  1.0f, 1.0f,
	// left bottom front
	-1.0f, -1.0f, 1.0F,
	// right bottom front
	 1.0f, -1.0f, 1.0f,
	// right top back
	 1.0f,  1.0f, -1.0f,
	// left top back
	-1.0f,  1.0f, -1.0f,
	// left bottom back
	-1.0f, -1.0f, -1.0f,
	// right bottom back
	 1.0f, -1.0f, -1.0f,
    };

    private float[] vert_colours = {

	// front face 
	1.0f, 0.0f, 0.0f,
	// back face
	0.5f, 0.0f, 1.0f,
	// right face
	0.0f, 1.0f, 1.0f,
	// left face
	0.5f, 1.0f, 1.0f,
	// top face
	0.0f, 1.0f, 0.0f,
	// bottom face
	0.5f, 1.0f, 0.0f,
    };

    private float[] vert_normals = {

	// front face 
	 0.0f,  0.0f,  1.0f,
	// back face
	 0.0f,  0.0f, -1.0f,
	// right face
	 1.0f,  0.0f,  0.0f,
	// left face
	-1.0f,  0.0f,  0.0f,
	// top face
	 0.0f,  1.0f,  0.0f,
	// bottom face
	 0.0f, -1.0f,  0.0f,
    };

    private float[] faces;
    private float[] face_normals;
    private float[] face_colours;

    public FloorCeiling () {

	//super(24, 
              //QuadArray.COORDINATES | QuadArray.NORMALS | QuadArray.COLOR_3);
	super(8, 
              QuadArray.COORDINATES | QuadArray.NORMALS);

	faces = new float[24];
	face_normals = new float[24];
	face_colours = new float[24];
        initialise ();
    }

    private void initialise () {

        setupFaces ();
        setupFaceNormals ();
	setupFaceColors ();
	setCoordinates(0, faces);
	setNormals(0, face_normals);
	//setColors(0, face_colours);
    }

    private void setupFaces () {

	// top
	copy_point (faces, 4*top_idx, verts, rtf_idx);
	copy_point (faces, 4*top_idx + 1, verts, rtb_idx);
	copy_point (faces, 4*top_idx + 2, verts, ltb_idx);
	copy_point (faces, 4*top_idx + 3, verts, ltf_idx);
	// bottom
	copy_point (faces, 4*bottom_idx, verts, rbf_idx);
	copy_point (faces, 4*bottom_idx + 1, verts, lbf_idx);
	copy_point (faces, 4*bottom_idx + 2, verts, lbb_idx);
	copy_point (faces, 4*bottom_idx + 3, verts, rbb_idx);
    }

    private void setupFaceNormals () {

        for (int face = 0; face < 2; face++) {

	    for (int vert = 0; vert < 4; vert++) {

		int cur_vert = face * 4 + vert;
	 	int cur_idx = cur_vert * 3;
	        face_normals[cur_idx] = vert_normals[face * 3];
	        face_normals[cur_idx + 1] = vert_normals[face * 3 + 1];
	        face_normals[cur_idx + 2] = vert_normals[face * 3 + 2];
	    }
	}
    }

    private void setupFaceColors () {

        for (int face = 0; face < 2; face++) {

	    for (int vert = 0; vert < 4; vert++) {

		int cur_vert = face * 4 + vert;
	 	int cur_idx = cur_vert * 3;
	        face_colours[cur_idx] = vert_colours[face * 3];
	        face_colours[cur_idx + 1] = vert_colours[face * 3 + 1];
	        face_colours[cur_idx + 2] = vert_colours[face * 3 + 2];
	    }
	}
    }
    
    
    void generatePlane(){
    	int VERTEX_COUNT = 20;
    	Point3f[] coords = new Point3f[VERTEX_COUNT];
    	for (int i = 0; i < VERTEX_COUNT; i++){
    		coords[i] = new Point3f(0, 0, 0);
    	}
    	TriangleArray ta = new TriangleArray(VERTEX_COUNT, 0);
    	ta.setCoordinates(0, coords);
    }
    

    private void copy_point (float[] dst, int dst_idx,float[] src, int src_idx) {

    	dst[3*dst_idx] = src[3*src_idx];
    	dst[3*dst_idx + 1] = src[3*src_idx + 1];
    	dst[3*dst_idx + 2] = src[3*src_idx + 2];

        if (DEBUG) {
        	System.out.println ("dst_idx = " + dst_idx + ", src_idx = " + src_idx);
        	System.out.println ("[" + src[3*src_idx] + "," + src[3*src_idx + 1] +
				"," + src[3*src_idx + 2] + "]");
        }
    }
}
